package com.softWang.system.controller;

import com.softWang.common.aop.annotation.SysOperationLog;
import com.softWang.common.request.Keyparam;
import com.softWang.common.response.PageBean;
import com.softWang.common.request.Idparam;
import com.softWang.common.response.ResponseResult;
import com.softWang.system.constants.SysModel;
import com.softWang.system.controller.vo.systemParameter.*;
import com.softWang.system.po.SystemParameter;
import com.softWang.system.service.SystemParameterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@RestController
@Api(value="参数管理",tags="参数管理")
@RequestMapping("/system/parameter")
public class SystemParameterController {
    @Resource
    private SystemParameterService parameterService;

    @PreAuthorize("@ss.hasPermi('system:parameter:query')")
    @ApiOperation(value="参数列表(分页)")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public ResponseResult<PageBean<SystemParameterQueryAllResult>> queryall(@RequestBody @Validated SystemParameterQueryAllParam param, HttpServletRequest request) {
        List<SystemParameterQueryAllResult> results=parameterService.queryall(param);
        PageBean page=new PageBean(results);
        return ResponseResult.success(page);
    }

    @PreAuthorize("@ss.hasPermi('system:parameter:add')")
    @ApiOperation(value="添加参数")
    @SysOperationLog(value = "添加参数", model = SysModel.PARAMS_MANAGE)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    //@Repeat(message = "重复提交")
    public ResponseResult<Long> add(@RequestBody @Validated SystemParameterAddParam param, HttpServletRequest request) throws Exception {
        SystemParameter systemParameter = new SystemParameter();
        BeanUtils.copyProperties(param, systemParameter);
        Long id = parameterService.add(systemParameter);
        return ResponseResult.success(id);
    }

    @PreAuthorize("@ss.hasPermi('system:parameter:edit')")
    @ApiOperation(value="修改参数")
    @SysOperationLog(value = "修改参数", model = SysModel.PARAMS_MANAGE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResponseResult<Long> edit(@RequestBody @Validated SystemParameterEditParam param, HttpServletRequest request) {
        SystemParameter systemParameter = new SystemParameter();
        BeanUtils.copyProperties(param, systemParameter);
        Long id = parameterService.edit(systemParameter);
        return ResponseResult.success(id);
    }

    @PreAuthorize("@ss.hasPermi('system:parameter:detail')")
    @ApiOperation(value="查看详情")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public ResponseResult<SystemParameterDetailResult> detail(@RequestBody @Validated Idparam idparam) {
        SystemParameterDetailResult result = new SystemParameterDetailResult();
        SystemParameter systemParameter = parameterService.getById(idparam.getId());
        BeanUtils.copyProperties(systemParameter, result);
        return ResponseResult.success(result);
    }

    @PreAuthorize("@ss.hasPermi('system:parameter:del')")
    @ApiOperation(value="删除")
    @SysOperationLog(value = "删除参数", model = SysModel.PARAMS_MANAGE)
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ResponseResult del(@RequestBody @NotEmpty(message = "ids 不能为空") List<@NotNull(message = "ids 不能为空") Long> ids, HttpServletRequest request) {
        parameterService.del(ids);
        return ResponseResult.success();
    }

    @ApiOperation(value="根据参数Key获取参数")
    @RequestMapping(value = "/getByKey", method = RequestMethod.POST)
    public ResponseResult<SystemParameterDetailResult> getByKey(@RequestBody @Validated Keyparam keyparam) {
        SystemParameterDetailResult result = new SystemParameterDetailResult();
        SystemParameter systemParameter = parameterService.getByKey(keyparam.getKey());
        BeanUtils.copyProperties(systemParameter, result);
        return ResponseResult.success(result);
    }

}
